﻿using Microsoft.Xna.Framework;
using SolarWinds.Helpers;

namespace SolarWinds.Particles
{
    public class LineParticleEmitter : ParticleEmitter
    {
        public LineParticleEmitter(ParticleEmitterDescription description) : base(description)
        {
        }

        protected override void InitParticle(ref Particle p)
        {
            // random point on line by "lerp"
            float t = RandomHelper.RandomFloat();
            Vector3 randPoint = t * mDescription.P0 + (1.0f - t) * mDescription.P1;

            // set position and velocity
            p.PreviousPosition = mDescription.Position + randPoint;
            p.Position = p.PreviousPosition;
            p.Velocity = mDescription.Direction * mDescription.Speed;

            // spread particle direction by rotation of velocity vector
            float spreadRad = MathHelper.ToRadians(mDescription.Spread);
            p.Velocity = Vector3.Transform(p.Velocity, Matrix.CreateRotationX(spreadRad * RandomHelper.RandomFloat2()));
            p.Velocity = Vector3.Transform(p.Velocity, Matrix.CreateRotationY(spreadRad * RandomHelper.RandomFloat2()));
            p.Velocity = Vector3.Transform(p.Velocity, Matrix.CreateRotationZ(spreadRad * RandomHelper.RandomFloat2()));
        }
    }
}
